import { Dep } from './Dep.js';

// 观察者:保存更新函数，值发生变化调用更新函数

export class Watcher {
  constructor(vm, key, updateFn) {
    this.vm = vm;
    this.key = key;
    this.updateFn = updateFn;

    // Dep.target静态属性上设置为当前watcher实例
    Dep.target = this;
    console.log('new Watcher :: 触发了getter');
    this.vm[this.key]; // 读取触发了getter
    Dep.target = null; // 收集完就置空
  }

  update() {
    this.updateFn.call(this.vm, this.vm[this.key]);
  }
}
